/*
 * Copyright 2011-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.platform.concurrent.data.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import me.ijleex.platform.concurrent.data.entity.Site;

/**
 * Query <i>Alexa Top 1-Million</i> Site Mapper
 *
 * @author liym
 * @since 2019-03-27 11:15 新建
 */
@Mapper
public interface Top1mSiteMapper {

    /**
     * 插入网址
     *
     * @param site 网址信息
     * @return 插入的数量
     * @since 2019-04-08 16:53
     */
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Integer.class, databaseId = "mysql")
    @SelectKey(statement = "SELECT CURRVAL(PG_GET_SERIAL_SEQUENCE('t_top1m_site', 'id'));", keyProperty = "id", before = false, resultType = Integer.class, databaseId = "postgres")
    @Insert("INSERT INTO t_top1m_site (site) VALUES (#{site})") // RETURNING id
    int insertSite(Site site);

    /**
     * 删除网址
     *
     * @param id ID
     * @return 删除的数量
     * @since 2019-04-08 20:40
     */
    @Delete("DELETE FROM t_top1m_site WHERE id=#{id}")
    int deleteSiteById(@Param("id") Integer id);

    /**
     * 删除网址
     *
     * @param site 网址
     * @return 删除的数量
     * @since 2019-04-08 21:14
     */
    @Delete("DELETE FROM t_top1m_site WHERE site=#{site}")
    int deleteSite(@Param("site") String site);

    /**
     * 更新网址
     *
     * @param site 网址信息，ID 和 site 不能为空
     * @return 更新的数量
     * @since 2019-04-08 20:43
     */
    @Update("UPDATE t_top1m_site SET site=#{site} WHERE id=#{id}")
    int updateSiteById(Site site);

    /**
     * 查询网址（所有）
     *
     * @return 网址列表
     */
    @Select("SELECT id,site,creator,create_time FROM t_top1m_site ORDER BY id ASC")
    List<Site> getSites();

    /**
     * 查询网址（单个）
     *
     * @param id ID
     * @return 网址
     */
    @Select("SELECT id,site,creator,create_time FROM t_top1m_site WHERE id=#{id}")
    Site getSiteById(@Param("id") Integer id);

}
